package cache_service

import (
	"errors"
	"interview/models"
	"interview/pkg/common"
	"interview/pkg/global"
	"interview/pkg/request"
	"interview/pkg/utils"
	"strings"
)

// LoginCheck 用户登录校验
func (s *RedisService) LoginCheck(user *models.User) (*models.User, error) {
	if !global.Conf.System.EnableRedis {
		return s.mysql.LoginCheck(user)
	}
	var u models.User
	var role models.SysRole

	jsonUsers := s.GetListFromCache(nil, u.TableName())
	i, err := JsonQueryFindOne(s.JsonQuery().FromString(jsonUsers).Where("username", "=", user.Username))
	if err != nil {
		return nil, err
	}
	utils.Struct2StructByJson(i, &u)

	jsonRoles := s.GetListFromCache(nil, role.TableName())
	i, err = JsonQueryFindOne(s.JsonQuery().FromString(jsonRoles).Where("id", "=", int(u.RoleId)))
	if err != nil {
		return nil, err
	}
	utils.Struct2StructByJson(i, &role)

	u.Role = role

	// 校验密码
	if ok := utils.ComparePassword(user.Password, u.Password); !ok {
		return nil, errors.New(common.LoginCheckErrMsg)
	}
	return &u, nil
}

// GetUserList 获取用户列表
func (s *RedisService) GetUserList(req *request.UserListReq) ([]models.User, uint, error) {
	if !global.Conf.System.EnableRedis {
		return s.mysql.GetUserList(req)
	}
	var userList []models.User

	jsonUsers := s.GetListFromCache(nil, new(models.User).TableName())
	jsonq := s.JsonQuery().FromString(jsonUsers)

	if req.Username = strings.TrimSpace(req.Username); req.Username != "" {
		jsonq = jsonq.Where("username", "contains", req.Username)
	}
	if req.Mobile = strings.TrimSpace(req.Mobile); req.Mobile != "" {
		jsonq = jsonq.Where("mobile", "contains", req.Mobile)
	}
	// TODO email等查询
	if req.Creator = strings.TrimSpace(req.Creator); req.Creator != "" {
		jsonq = jsonq.Where("creator", "contains", req.Creator)
	}
	if req.Status != nil {
		jsonq = jsonq.Where("status", "=", *req.Status)
	}
	total := uint(jsonq.Count())
	limit, offset := req.GetLimit()
	res := jsonq.Limit(int(limit)).Offset(int(offset)).Get()
	utils.Struct2StructByJson(res, &userList)
	return userList, total, nil
}

// GetUserInfo 查询用户信息
func (s *RedisService) GetUserInfo(ID uint) (*models.User, error) {
	if !global.Conf.System.EnableRedis {
		return s.mysql.GetUserInfo(ID)
	}
	var user models.User
	var role models.SysRole
	// 获取用户和角色
	_ = s.GetItemByIdFromCache(ID, &user, user.TableName())
	jsonRoles := s.GetListFromCache(nil, role.TableName())
	one, err := JsonQueryFindOne(s.JsonQuery().FromString(jsonRoles).Where("id", "=", int(user.RoleId)))
	if err != nil {
		return nil, err
	}
	utils.Struct2StructByJson(one, &role)
	user.Role = role

	return &user, nil
}

// GetUserListByRoleId 根据角色id查询对应的用户列表
func (s *RedisService) GetUserListByRoleId(req *request.UserListReq) ([]models.User, uint, error) {
	if !global.Conf.System.EnableRedis {
		return s.mysql.GetUserListByRoleId(req)
	}
	var userList []models.User
	var total uint
	limit, offset := req.GetLimit()

	jsonUsers := s.GetListFromCache(nil, new(models.User).TableName())

	jsonq := s.JsonQuery().FromString(jsonUsers).Where("roleId", "=", int(req.RoleId))

	total = uint(jsonq.Count())
	res := jsonq.Limit(int(limit)).Offset(int(offset)).Get()

	utils.Struct2StructByJson(res, &userList)

	return userList, total, nil
}
